/**
 * 
 */
package com.tdkj.action.front.mall.shop;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.decorate.BeanLayout;
import com.tdkj.model.decorate.BeanLayoutModuleConfig;
import com.tdkj.model.decorate.BeanModule;
import com.tdkj.model.decorate.BeanStyle;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.model.shop.BeanOrderItem;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.model.shop.BeanShopComment;
import com.tdkj.model.shop.BeanShopDecorateInfo;
import com.tdkj.model.shop.BeanShopNotice;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.common.IBeanTypeService;
import com.tdkj.service.shop.IBeanCreditRatingService;
import com.tdkj.service.shop.IBeanGoodService;
import com.tdkj.service.shop.IBeanShopService;
import com.tdkj.util.Page;
import com.tdkj.util.PublicConfing;

/**
 * @author yninfo
 */
public class ShopAction extends BaseAction {
	
	private Long sid ;//shop id
	private Long uid ;//user id
	private Long gid ;//good id
	private Long innerTypeId;
	private Long userId ;
	
	//shop\notice\good\comment\credit\photo\innerType\search
	private String showType = "shop";
	private BeanShop shop ;
	private BeanGood good ;
	private IBeanShopService shopService;
	
	private IBeanGoodService goodService;
	
	private IBeanCreditRatingService creditRatingService;
	
	private IBeanTypeService typeService;
	
	private BeanStyle style;
	
	private BeanLayout layout;
	
	private BeanShopComment comment;
	
	public String execute(){
		if(this.getCurrentUser() != null){
			ActionContext.getContext().getSession().put(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG, shopService.findById(this.getCurrentUser().getId(), BeanUser.class));
		}
		
		if(gid != null){
			good = goodService.getGoodById(gid);
			uid = good.getUser().getId();
			
			String itemsHql = " from " + BeanOrderItem.class.getName() + " as bean where bean.good.id='"+gid+"' ";
			Query itemsQuery = this.createQuery(itemsHql);
			
			Page items = this.getBaseService().findBy(itemsQuery, 1, 10);
			this.getReturnInfo().put("items", items);
			
			
			String buyerEvaluateHql = "select bean.buyerEvaluate from " + BeanOrderItem.class.getName() + " as bean where bean.good.id='"+gid+"' and bean.isBuyerEvaluated='1' ";
			Query buyerEvaluateQuery = this.createQuery(buyerEvaluateHql);
			
			this.getReturnInfo().put("buyerEvaluates", buyerEvaluateQuery.list());
			
			showType = "goodDetail";
		}
		
		if(sid != null){
			shop = shopService.findById(sid, BeanShop.class);
			uid = shop.getUser().getId();
		}
		
		BeanUser user = shopService.findById(uid, BeanUser.class);
		
		shop = user.getShop();
		//shopService.deleteDecorateInfo(id);
		
		if(shop.getDecorateInfo() == null){
			{
				Map<BeanLayout, BeanLayoutModuleConfig> configs = new HashMap<BeanLayout, BeanLayoutModuleConfig>();
				BeanLayout layout = shopService.findById(1L, BeanLayout.class);
				
				BeanLayout layout1 = layout.getChildren().get(0);
				BeanLayout layout4 = layout.getChildren().get(1);
				
				List<BeanModule> modules1 = new ArrayList<BeanModule>();
				modules1.add(shopService.findById(1L, BeanModule.class));
				modules1.add(shopService.findById(2L, BeanModule.class));
				modules1.add(shopService.findById(3L, BeanModule.class));
				BeanLayoutModuleConfig config1 = new BeanLayoutModuleConfig();
				config1.setLayout(layout1);
				config1.setModules(modules1);
				
				
				List<BeanModule> modules4 = new ArrayList<BeanModule>();
				modules4.add(shopService.findById(6L, BeanModule.class));
				modules4.add(shopService.findById(7L, BeanModule.class));
				modules4.add(shopService.findById(8L, BeanModule.class));
				BeanLayoutModuleConfig config4 = new BeanLayoutModuleConfig();
				config4.setLayout(layout4);
				config4.setModules(modules4);
				
				configs.put(layout1, config1);
				configs.put(layout4, config4);
				
				BeanStyle style = shopService.findById(2L, BeanStyle.class);
				BeanShopDecorateInfo decorateInfo = new BeanShopDecorateInfo();
				decorateInfo.setLayout(layout);
				decorateInfo.setConfigs(configs);
				decorateInfo.setStyle(style);
				shopService.save(decorateInfo);
				
				shop.setDecorateInfo(decorateInfo);
				shopService.saveOrUpdate(shop, BeanShop.class);
			}
			
			{
				Map<BeanLayout, BeanLayoutModuleConfig> configs = new HashMap<BeanLayout, BeanLayoutModuleConfig>();
				BeanLayout layout = shopService.findById(1L, BeanLayout.class);
				
				BeanLayout layout1 = layout.getChildren().get(0);
				BeanLayout layout4 = layout.getChildren().get(1);
				
				List<BeanModule> modules1 = new ArrayList<BeanModule>();
				modules1.add(shopService.findById(1L, BeanModule.class));
				modules1.add(shopService.findById(2L, BeanModule.class));
				modules1.add(shopService.findById(3L, BeanModule.class));
				BeanLayoutModuleConfig config1 = new BeanLayoutModuleConfig();
				config1.setLayout(layout1);
				config1.setModules(modules1);
				
				
				List<BeanModule> modules4 = new ArrayList<BeanModule>();
				modules4.add(shopService.findById(6L, BeanModule.class));
				modules4.add(shopService.findById(7L, BeanModule.class));
				modules4.add(shopService.findById(8L, BeanModule.class));
				BeanLayoutModuleConfig config4 = new BeanLayoutModuleConfig();
				config4.setLayout(layout4);
				config4.setModules(modules4);
				
				configs.put(layout1, config1);
				configs.put(layout4, config4);
				
				BeanStyle style = shopService.findById(2L, BeanStyle.class);
				BeanShopDecorateInfo decorateInfo = new BeanShopDecorateInfo();
				decorateInfo.setLayout(layout);
				decorateInfo.setConfigs(configs);
				decorateInfo.setStyle(style);
				shopService.save(decorateInfo);
				
				shop.setDecorateInfoTemp(decorateInfo);
				shopService.saveOrUpdate(shop, BeanShop.class);
			}
			
		}
		
		
		String rootName = IBeanTypeService.Constants.getShopGoodTypeRoot(uid);
		this.getReturnInfo().put("innerTypeRoot", typeService.getRoot(rootName));
		this.getReturnInfo().put("creditRatings", creditRatingService.getAllCreditRatings());
		
		if("shop".equalsIgnoreCase(showType)){
			Query commentsQuery = shopService.getBaseDao().getSession()
			.createQuery("from " + BeanShopComment.class.getName() + " as bean where bean.shop.id="+shop.getId());
			Page comments = this.getBaseService().getBaseDao().findBy(commentsQuery, 1, 10);
			this.getReturnInfo().put("comments", comments);
		} else if ("notice".equalsIgnoreCase(showType)){
			Query noticesQuery = shopService.getBaseDao().getSession()
			.createQuery("from " + BeanShopNotice.class.getName() + " as bean where bean.user.id="+uid + " order by bean.publishTime desc");
			Page notices = this.getBaseService().getBaseDao().findBy(noticesQuery, 1, 1);
			this.getReturnInfo().put("notices", notices);
			
		} else if ("comment".equalsIgnoreCase(showType)){
			Query commentsQuery = shopService.getBaseDao().getSession()
			.createQuery("from " + BeanShopComment.class.getName() + " as bean where bean.shop.id="+shop.getId());
			Page comments = this.getBaseService().getBaseDao().findBy(commentsQuery, 1, 10);
			this.getReturnInfo().put("comments", comments);
		} else if ("credit".equalsIgnoreCase(showType)){
		} else if ("photo".equalsIgnoreCase(showType)){
		} else if ("innerType".equalsIgnoreCase(showType)){
			String hql = "from " + BeanGood.class.getName() + " as bean where 1=1 ";
			if(innerTypeId != null){
				hql = hql + " and bean.innerType.id="+innerTypeId;
			}
			Query innerTypeGoodsQuery = shopService.getBaseDao().getSession()
			.createQuery(hql);
			this.getReturnInfo().put("innerTypeGoods", innerTypeGoodsQuery.list());
		} else if ("search".equalsIgnoreCase(showType)){
			String keywords= this.getQueryInfo().get("keywords");
			String hql = "from " + BeanGood.class.getName() + " as bean where 1=1 ";
			if(keywords != null && !"".equals(keywords)){
				hql = hql + " and (bean.name like '%"+keywords+"%' or bean.keywords like '%"+keywords+"%' or bean.detailDescription like '%"+keywords+"%') ";
			}
			Query searchResultQuery = shopService.getBaseDao().getSession()
			.createQuery(hql);
			this.getReturnInfo().put("searchResult", searchResultQuery.list());
		}
		
		return "success";
	}
	
	public String decorate(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		uid = user.getId();
		execute();
		return "decorate";
	}
	
	public String changeStyle(){
		BeanUser user = shopService.findById(uid, BeanUser.class);
		shop = user.getShop();
		
		style = shopService.findById(style.getId(), BeanStyle.class);
		shop.getDecorateInfoTemp().setStyle(style);
		
		shopService.saveOrUpdate(shop.getDecorateInfoTemp(), BeanShopDecorateInfo.class);
		return decorate();
	}
	
	public String changeLayout(){
		
		shopService.changeLayout(uid, layout.getId());
		return decorate();
	}
	
	public String saveDecorate(){

		shopService.saveDecorateInfo(uid);
		return decorate();
	}
	
	public String comment(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		comment.setUser(user);
		comment.setTime(new Timestamp(System.currentTimeMillis()));
		shopService.save(comment);
		
		sid = comment.getShop().getId();
		return execute();
	}

	public BeanShop getShop() {
		return shop;
	}

	public void setShop(BeanShop shop) {
		this.shop = shop;
	}

	@Resource
	public void setShopService(IBeanShopService shopService) {
		this.shopService = shopService;
	}

	@Resource
	public void setCreditRatingService(IBeanCreditRatingService creditRatingService) {
		this.creditRatingService = creditRatingService;
	}


	@Resource
	public void setTypeService(IBeanTypeService typeService) {
		this.typeService = typeService;
	}

	public Long getInnerTypeId() {
		return innerTypeId;
	}

	public void setInnerTypeId(Long innerTypeId) {
		this.innerTypeId = innerTypeId;
	}

	public String getShowType() {
		return showType;
	}

	public void setShowType(String showType) {
		this.showType = showType;
	}

	public BeanStyle getStyle() {
		return style;
	}

	public void setStyle(BeanStyle style) {
		this.style = style;
	}

	public BeanLayout getLayout() {
		return layout;
	}

	public void setLayout(BeanLayout layout) {
		this.layout = layout;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public BeanShopComment getComment() {
		return comment;
	}

	public void setComment(BeanShopComment comment) {
		this.comment = comment;
	}

	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public Long getGid() {
		return gid;
	}

	public void setGid(Long gid) {
		this.gid = gid;
	}

	public Long getSid() {
		return sid;
	}

	public void setSid(Long sid) {
		this.sid = sid;
	}

	public BeanGood getGood() {
		return good;
	}

	public void setGood(BeanGood good) {
		this.good = good;
	}

	@Resource
	public void setGoodService(IBeanGoodService goodService) {
		this.goodService = goodService;
	}
	
}
